<template>
  <div>
    <my-breadcrumb level1="商品管理" level2="商品列表"></my-breadcrumb>
    <!-- 1.0添加商品分类按钮 -->
    <el-button type="success" plain @click="addCategories">添加分类</el-button>
    <dzh-tree-grid :columns="columns" :loading="loading" :dataSource="categories">
      <template slot-scope="scope">
        <el-button
          plain
          title="修改商品分类"
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="editCategory(scope.row)"
        ></el-button>
        <el-button
          plain
          title="删除商品分类"
          type="danger"
          size="small"
          icon="el-icon-delete"
          @click="deleteCategory(scope.row.cat_id)"
        ></el-button>
      </template>
    </dzh-tree-grid>
    <!-- 2.0 分页条 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 3.0添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible4Add" width="50%">
      <el-form :model="addCategoriesObj" :rules="rules" ref="addCategoryForm" label-width="100px">
        <el-form-item label="添加分类" prop="cat_name">
          <el-input v-model="addCategoriesObj.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类名称">
          <!-- 放级联菜单选择器 -->
          <el-cascader
            placeholder="不选默认是一级"
            :options="options"
            change-on-select
            v-model="selectedOptions"
            :props="defaultProps"
            @change="addCascaderChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible4Add = false">取 消</el-button>
        <el-button type="primary" @click="submitCategories">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 4.0添加修改对话框 -->
    <el-dialog title="修改分类" :visible.sync="dialogVisible4Edit" width="50%">
      <el-form :model="editCategoriesObj" :rules="rules" label-width="100px">
        <el-form-item label="修改分类" prop="cat_name">
          <el-input v-model="editCategoriesObj.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible4Edit = false">取 消</el-button>
        <el-button type="primary" @click="submitEditCategories">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
//导入面包屑子组件
import MyBreadcrumb from '../subcomponents/MyBreadcrumb'
import DZHTreeGrid from 'dzh-tree-grid'
export default {
  //注册子组件
  components: {
    'my-breadcrumb': MyBreadcrumb,
    //注册树形结构组件
    'dzh-tree-grid': DZHTreeGrid
  },
  data() {
    return {
      pagenum: 1,
      pagesize: 10,
      total: this.total,
      loading: true,
      dialogVisible4Add: false,
      dialogVisible4Edit: false,
      selectedOptions: [],
      columns: [
        {
          //需要展示的列

          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '级别',
          prop: 'cat_level_name',
          width: 500 // 可选
        }
      ],
      categories: [
        {
          //需要展示的表格数据
        }
      ],
      addCategoriesObj: {
        //添加分类的数据
        cat_name: '', //分类名字
        cat_pid: '', //父级分类
        cat_level: 0 //一级伪0,二级为1,三级为2
      },
      editCategoriesObj: {
        //修改分类数据
        cat_id: 0,
        cat_name: ''
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      options: [], //级联菜单数据
      defaultProps: {
        // 配置映射关系
        label: 'cat_name',
        children: 'children',
        value: 'cat_id'
      }
    }
  },
  created() {
    this.getCategoriesDate()
  },
  methods: {
    getCategoriesDate() {
      //发送请求
      this.$axios
        .get(`categories`, {
          params: {
            type: 3,
            pagenum: this.pagenum,
            pagesize: this.pagesize
          }
        })
        .then(res => {
          this.loading = false
          //   赋值给模型
          this.categories = res.data.data.result
          //赋值总条数
          this.total = res.data.data.total
        })
    },
    //当页面改变的时候触发
    handleSizeChange(pagesize) {
      this.pagesize = pagesize
      this.getCategoriesDate()
    },
    handleCurrentChange(pagenum) {
      this.pagenum = pagenum
      this.getCategoriesDate()
    },
    //添加分类
    addCategories() {
      //要判断最后是否输入值用$refs
      this.$axios
        .get(`categories`, {
          params: {
            type: 2
          }
        })
        .then(res => {
          console.log(res)
          //   赋值给模型
          this.options = res.data.data
          this.dialogVisible4Add = true
        })
    },
    //分类改变了
    addCascaderChange(val) {
      if (val.length == 1) {
        //说明选择了一级分类 添加二级
        this.addCategoriesObj.cat_pid = val[0]
        this.addCategoriesObj.cat_level = 1
      } else if (val.length == 2) {
        //说明选择了二级分类 添加三级
        this.addCategoriesObj.cat_pid = val[1]
        this.addCategoriesObj.cat_level = 2
      } else {
        this.addCategoriesObj.cat_pid = 0
        this.addCategoriesObj.cat_level = 0
      }
    },
    // 最终提交分类
    submitCategories() {
      //最终提交时验证有没有输入值
      // 拿到el-form这个表单
      this.$refs.addCategoryForm.validate(valid => {
        if (valid) {
          // 验证通过
          this.$axios.post(`categories`, this.addCategoriesObj).then(res => {
            if (res.data.meta.status === 201) {
              // 隐藏对话框
              this.dialogVisible4Add = false

              // 提示
              this.$message({
                type: 'success',
                message: res.data.meta.msg
              })

              // 重新查询列表
              this.getCategoriesDate()
            }
          })
        }
      })
    },
    // 修改分类 并弹出对话框
    editCategory(obj) {
      this.editCategoriesObj.cat_id = obj.cat_id
      this.editCategoriesObj.cat_name = obj.cat_name
      this.dialogVisible4Edit = true
    },
    //修改完最终提交
    submitEditCategories() {
      this.$axios
        .put(
          `categories/${this.editCategoriesObj.cat_id}`,
          this.editCategoriesObj
        )
        .then(res => {
          if (res.data.meta.status !== 200) {
            //提示修改失败
            this.$message.error(res.data.meta.msg)
          } else {
            this.$message({
              message: '修改用户信息成功',
              type: 'success'
            })
            //关闭对话框
            this.dialogVisible4Edit = false
            //刷新列表
            this.getCategoriesDate()
          }
        })
    },
    //删除分类
    deleteCategory(cat_id) {
      this.$confirm('是否要删除页面?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        this.$axios.delete(`categories/${cat_id}`).then(res => {
        //   console.log(res)
          if (res.data.meta.status === 200) {
            //1、提示
            this.$message({
              message: res.data.meta.msg,
              type: 'success'
            })
            //2、重新请求，获取数据
            this.getCategoriesDate()
          }
           
        })
      })
    }
  }
}
</script>
